<?php
declare (strict_types=1);

namespace app\common\listener;

use app\common\model\Config;
use app\common\model\OrderProduct;
use app\common\model\Relation;
use app\common\model\User;
use app\common\tool\account\Account;
use app\common\model\Account as AccountModel;
use think\Exception;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Log;

class SupplyAward
{
    /**
     * 直推、间推、供应链奖励在确认收货7天后的凌晨进行结算,民宿酒店、本地生活核销后结算
     * 供应链奖励
     * @param $data
     */
    public function handle($order)
    {
        try{
            $config = Config::where('group',10)->column('value','name');
            OrderProduct::with('product')->where('order_id',$order->id)->select()->each(function ($item) use ($config){
                if($item->product->uid && $item->profit>0 && !in_array($item->cate_1,[2, 9, 10])){//除了店主、定制、合约以外的都算消费区,合约区暂时未定
                    $this->supply($item->product->user, $item, $config);
                }
            });
        }catch (\Exception $e){
            Log::write("listener.SupplyAward:".$e->getMessage());
            throw new Exception("加载失败：".$e->getMessage(), -1);
        }
    }

    private function supply($user,$op,$config)
    {
        $rate = [
            '3' => $config['consume_supply_shoper_rate'],
            '4' => $config['consume_supply_shoper_rate'],
            '5' => $config['consume_supply_leader_rate'],
            '6' => $config['consume_supply_district_rate'],
            '7' => $config['consume_supply_city_rate'],
            '8' => $config['consume_supply_city_rate']
        ];

        $cate = [
            '3' => AccountModel::CONSUM_SUPPLY_SHOPER,
            '4' => AccountModel::CONSUM_SUPPLY_SHOPER,
            '5' => AccountModel::CONSUM_SUPPLY_LEADER,
            '6' => AccountModel::CONSUM_SUPPLY_DISTRICT,
            '7' => AccountModel::CONSUM_SUPPLY_CITY,
            '8' => AccountModel::CONSUM_SUPPLY_CITY,
        ];

        $account = 'love_bean';
        $note = '消费区供应链奖励';

        Relation::opZoneBonus($user,$op,$rate,$account,$cate,$note,true);
    }

}